package demo.configs;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

@Configuration
public class GatewayConfiguration {
    @Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
//                再这里写路由规则
                .route("u1", r -> r.path("/getServer2/**")
                        .filters(f -> f.rewritePath("/getServer1/aa", "getServer/bb"))
                        .uri("lb://hadage")).build();
    }

    @Bean
    KeyResolver ipKeyResolver() {
        return exchange -> {
            System.out.println("RemoteAddress " + exchange.getRequest().getRemoteAddress());
            System.out.println("Address " + exchange.getRequest().getRemoteAddress().getAddress());
            System.out.println("HostAddress " + exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
            return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
        };
    }

}
